import { Animation, Component } from "cc";
import { State } from "./State";
import { ParamsTypeEnum } from "../Enum";
import { StateTypeEnum } from "../Common";

type ParamsValueType = boolean | number
export interface IParamsValue {
    type: ParamsTypeEnum,
    value: ParamsValueType
}

export const getInitParamTrigger = () => {
    return {
        type: ParamsTypeEnum.trigger,
        value: false
    }
}

export abstract class StateMachine extends Component {

    private _currencyState: State = null

    params: Map<StateTypeEnum, IParamsValue> = new Map()
    stateMachines: Map<StateTypeEnum, State> = new Map()
    animationComponent: Animation = null

    getPararams(state: StateTypeEnum) {
        return this.params.get(state).value
    }

    setParams(state: StateTypeEnum, value: boolean) {
        if (this.params.has(state)) {
            this.params.get(state).value = value
            this.run()
            this.resetTrigger()
        }
    }

    get currencyState() {
        return this._currencyState
    }

    set currencyState(arg: State) {
        if (!arg) return
        this._currencyState = arg
        this._currencyState.run()
    }

    resetTrigger() {
        for (const [_, params] of this.params) {
            if (params.type === ParamsTypeEnum.trigger) {
                params.value = false
            }
        }
    }

    abstract init(...arg: any[]): void
    abstract run(): void
}